package com.chinatgl.latte.ec.launcher;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.chinatgl.latte.app.Latte;
import com.chinatgl.latte.delegates.LatteDelegate;
import com.chinatgl.latte.ec.R;
import com.chinatgl.latte.ec.R2;
import com.chinatgl.latte.ec.contains.Contains;
import com.chinatgl.latte.ec.main.EcBottomDelegate;
import com.chinatgl.latte.ec.sign.SignInDelegate;
import com.chinatgl.latte.util.dimen.DensityUtils;
import com.chinatgl.latte.util.storage.SpUtil;

import java.util.ArrayList;

import butterknife.BindView;

/**
 * 文件描述:
 * 作者: Created by 杨伟乔 on 2017/8/8 0008.
 * 版本号: 1.0
 */
@SuppressWarnings("all")
public class LauncherScrollDelegate extends LatteDelegate implements View.OnClickListener {

    @BindView(R2.id.vp_guide_bg)
    ViewPager vpGuideBg;
    @BindView(R2.id.ll_guide_points)
    LinearLayout llGuidePoints;  //放置圆点的线性布局
    @BindView(R2.id.iv_guide_bigpoint)
    ImageView ivGuideBigpoint; //滑动时候会移动的圆点
    @BindView(R2.id.bt_guide_start)
    TextView btGuideStart;  //开始按钮
    @BindView(R2.id.rl_point)
    RelativeLayout rlPoint;


    private ArrayList<ImageView> imgs;
    private int width;
//    @Override
//    public void onAttach(Activity activity) {
//        super.onAttach(activity);
//        if (activity instanceof ILauncherListener) {
//            mILauncherListener = (ILauncherListener) activity;
//        }
//    }

    @Override
    public Object setLayout() {
        return R.layout.delegate_guide;
    }

    @Override
    public void onBindView(@Nullable Bundle savedInstanceState, @NonNull View rootView) {

        btGuideStart.setOnClickListener(this);//监听开始按钮
        initData();
        initAdapter();

    }

    private void initAdapter() {
        // 设置Adapter
        vpGuideBg.setAdapter(new MyAdapter());
        // 监听ViewPager
        vpGuideBg.setOnPageChangeListener(new MyOnPageChangeListener());
    }

    private void initData() {
        //创建图片id的数组
        int[] imgid = new int[]{R.mipmap.guide1, R.mipmap.guide2,
                R.mipmap.guide3};
        // 根据图片id的数组创建图片集合
        imgs = new ArrayList<ImageView>();
        /**
         * 下面这个for循环 一共干了两件事
         * 1.创建图片控件 imageView的集合
         * 2.创建了圆点指示器的控件
         */
        for (int i = 0; i < imgid.length; i++) {
            //创建图片控件
            ImageView imageView = new ImageView(_mActivity);
            // 给ImageView设置缩放类型
            imageView.setScaleType(ImageView.ScaleType.FIT_XY);
            //设置图片
            imageView.setImageResource(imgid[i]);
            // 设置背景时，图片能自动按控件的大小缩放
            // imageView.setBackgroundResource(imgid[i]);
            imgs.add(imageView);

            // 有几张图片，创建几个灰点
            ImageView point = new ImageView(_mActivity);
            point.setImageResource(R.mipmap.qiehuan_nor);
            // 给灰点设置宽高,需要导包Linearlayout中的LayoutParams
            int dp2px = DensityUtils.dip2px(Latte.getApplication(), 10);// 根据手机获取15dp对应的px值
            int dp2px1 = DensityUtils.dip2px(Latte.getApplication(), 15);// 根据手机获取15dp对应的px值
            // LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) point.getLayoutParams();

            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(dp2px, dp2px);
            width = point.getWidth();
            // 设置左边距,第一个点的左边距不要，为了让中间的灰点显示在屏幕的水平居中
            if (i != 0) {
                params.leftMargin = dp2px1;
            }
            point.setLayoutParams(params);
            // 把灰点添加到线性容器
            llGuidePoints.addView(point);
        }

    }


    @Override
    public void onClick(View v) {
        //点击开始进入应用

        //1.保存记录 不是第一次进入了
        SpUtil.setBoolean(Contains.IS_FRIST, true);
        //2. 进入主界面
        startWithPop(new EcBottomDelegate());

    }


    class MyOnPageChangeListener implements ViewPager.OnPageChangeListener {

        @Override
        public void onPageScrolled(int position, float positionOffset,
                                   int positionOffsetPixels) {
            // 当ViewPager滑动时调用
            // 计算红点要移动的距离 = 手指移动的距离/屏幕的宽度 * 灰点的间距
            // 移动的距离 = positionOffset * 灰点的间距
            int bigPointX = (int) ((positionOffset + position) * (DensityUtils.dip2px(Latte.getApplication(), 25))); //30是自己计算出来的 15灰色圆点半径 +灰色圆点之间的间距
            // 移动红点，不停的修改红点的左边距
            RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) ivGuideBigpoint.getLayoutParams();
            params.leftMargin = bigPointX;
            ivGuideBigpoint.setLayoutParams(params);
        }

        @Override
        public void onPageSelected(int position) {
            // 当ViewPager滑动到某一页时调用
            if (position == imgs.size() - 1) {
                btGuideStart.setVisibility(View.VISIBLE);
                rlPoint.setVisibility(View.GONE);
            } else {
                btGuideStart.setVisibility(View.GONE);
                rlPoint.setVisibility(View.VISIBLE);
            }
        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }

    }

    /**
     * viewpager的适配器
     */
    class MyAdapter extends PagerAdapter {

        @Override
        public int getCount() {
            return imgs.size();
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            // 把对应位置的图片添加到ViewPager中
            ImageView imageView = imgs.get(position);
            container.addView(imageView);
            return imageView;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);
        }

    }
}
